package com.livepass.android.girlspicks.activities;

import habbot.sdc.delegate.FacebookLoginDelegate;
import habbot.sdc.delegate.ShareFacebookDelegate;
import habbot.sdc.delegate.SharekitDelegate;
import habbot.sdc.delegate.TwitterLoginDelegate;
import habbot.sdc.helper.ShareFacebookHelper;
import habbot.sdc.helper.ShareTwitterHelper;
import habbot.sdc.object.ShareMessage;
import habbot.sdc.object.ShareMessage.ShareType;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.livepass.android.girlspicks.MyApplication;
import com.livepass.android.girlspicks.R;
import com.livepass.android.girlspicks.models.Article;

public class PushCommentReplyActivity extends Activity {
	public static final String TW_API_KEY = "OPqZhRksEG6Thsp5YZpTMqZB7";
	public static final String TW_SECRET = "EJuOWYMeIdP3lmDs9qFfKUxm4KJZl1fQYUo9dsRE9lAqqTm2iB";
	
	boolean isTwPressed = false; //not press yet
	boolean isFbPressed = false; //not press yet
	int kindOfCommentOrReply;
	private Article article;
	
	Button btnShareTwitter;
	Button btnShareFacebook;
	EditText edtCommentInput;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_push_comment_reply);
		
		//get extra
		kindOfCommentOrReply = getIntent().getExtras().getInt(MyApplication.WHERE);
		//get article
		article = (Article) getIntent().getExtras().get("article");
		
		btnShareTwitter = (Button) findViewById(R.id.btnTwitter);
		btnShareFacebook = (Button) findViewById(R.id.btnFacebook);
		edtCommentInput = (EditText) findViewById(R.id.edtCommentInput);
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		ShareFacebookHelper.onActivityResult(this, requestCode, resultCode, data);
	}
	
	@Override
	protected void onDestroy() {
		try {
			InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
			inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
		} catch (Exception e) {
			Toast.makeText(getApplicationContext(), ""+e.getMessage(), Toast.LENGTH_SHORT).show();
		}
		finish();
		super.onDestroy();
	}
	
	public void doShareTwitter(View view) {
		if(isTwPressed) {
			isTwPressed = false;
		} else {
			isTwPressed = true;
			if (!ShareTwitterHelper.isTwitterConnected(this)) {
				ShareTwitterHelper.authorize(this, TW_API_KEY, TW_SECRET,
						"http://twitterapp.com", new TwitterLoginDelegate() {

							@Override
							public void onError(String error) {
								Toast.makeText(PushCommentReplyActivity.this,
										"Error: " + error, Toast.LENGTH_LONG)
										.show();
								doShareTwitter(null);
							}

							@Override
							public void onComplete() {
								Toast.makeText(PushCommentReplyActivity.this,
										"Connected to twitter", Toast.LENGTH_LONG)
										.show();
							}
						});
			}
		}
		btnShareTwitter.setSelected(isTwPressed);
	}

	public void doShareFacebook(View view) {
		if(isFbPressed) {
			isFbPressed = false;
		} else {
			isFbPressed = true;
			if (!ShareFacebookHelper.isFacebookConnected()) {
				ShareFacebookHelper.authorize(this, new FacebookLoginDelegate() {

					@Override
					public void onError(String error) {
						Toast.makeText(PushCommentReplyActivity.this, "Error: " + error,
								Toast.LENGTH_LONG).show();
						isFbPressed = false;
						btnShareFacebook.setSelected(isFbPressed);
					}

					@Override
					public void onComplete() {
						Toast.makeText(PushCommentReplyActivity.this, "Connected to twitter",
								Toast.LENGTH_LONG).show();
						isFbPressed = true;
						btnShareFacebook.setSelected(isFbPressed);

					}
				});
			}
		}
		btnShareFacebook.setSelected(isFbPressed);
	}
	
	public void doShare() {
		String link = MyApplication.APP_LINK_GLOBAL+"id="+article.getArticleId()+"&cat_id="+article.getCategoryId();
		if(isTwPressed) {
			ShareMessage message = new ShareMessage();
			message.setMessage(edtCommentInput.getText().toString().trim()+" \n"+link);
			ShareTwitterHelper.shareMessage(this, message, TW_API_KEY,
					TW_SECRET, new SharekitDelegate() {

						@Override
						public void onError(String message) {
							Toast.makeText(PushCommentReplyActivity.this, "Error: " + message, Toast.LENGTH_LONG).show();
						}

						@Override
						public void onComplete() {
							Toast.makeText(PushCommentReplyActivity.this,
									"Shared successfully!", Toast.LENGTH_LONG)
									.show();
						}
					});
		}
		if(isFbPressed) {
			ShareMessage message = new ShareMessage();
			message.setType(ShareType.STORY);
			//message.setLink(article.getOrgUrl());
			message.setLink(link);
			message.setCaption(article.getSite());
			message.setDescription(article.getOverview());
			message.setName(article.getTitle());
			message.setPicture(!"".equals(article.getImage())?article.getImage():"http://girlspicks.co/img/icon.png");
			message.setMessage(edtCommentInput.getText().toString().trim());
			ShareFacebookHelper.shareMessage(PushCommentReplyActivity.this, message,
					new ShareFacebookDelegate() {

						@Override
						public void onError(String message) {
							Toast.makeText(PushCommentReplyActivity.this,
									"Error: " + message, Toast.LENGTH_LONG)
									.show();
						}

						@Override
						public void onSuccess() {
							Toast.makeText(PushCommentReplyActivity.this,
									"Shared successfully!", Toast.LENGTH_LONG)
									.show();
						}
					});
		}
	}
	
	public void doCommentSend(View view) {
		if(!"".equals(edtCommentInput.getText().toString().trim())) {
			edtCommentInput.setEnabled(false);
			view.setEnabled(false);
			btnShareFacebook.setEnabled(false);
			btnShareTwitter.setEnabled(false);
			doShare();
			if(kindOfCommentOrReply == MyApplication.COMMENT) {
				String edtComment = edtCommentInput.getText().toString().trim();
				Intent in = new Intent();
				in.putExtra("comment", edtComment);
				setResult(RESULT_OK, in);
				finish();
				
			} else if(kindOfCommentOrReply == MyApplication.REPLY) {
				String edtComment = edtCommentInput.getText().toString().trim();
				Intent in = new Intent();
				in.putExtra("reply", edtComment);
				setResult(RESULT_OK, in);
				finish();
			}
		} else {
			Toast.makeText(getApplicationContext(), "入力してください。", Toast.LENGTH_SHORT).show();
			finish();
		}
	}
	
	@Override
	public void onBackPressed() {
		super.onBackPressed();
		finish();
	}
}
